const $axios = require("w-vue-middle/api/axios").default;

// todo... 接口需更换
/**
 * 获取模型表
 * @param {*} params
 */

export function getModelTable(params) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/dataModelManage/queryEditModelDetail`,
    method: "get",
    params
  });
}
/**
 * 表删除
 * @param {*} params 
 * @returns 
 */
export function deleteFieldMetadataNew(params) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/dataModelManage/deleteFieldData`,
    method: "get",
    params
  });
}

/**
 * 获取数据元
 * @param {*} params 
 * @returns 
 */
export function getDataElementList(
  params = {
    pageNumber: 1,
    pageSize: 10,
    name: undefined,
    uid: undefined
  }
) {
  return $axios({
    url: `${$service.dataModelService}/api/registerMetadata/dataElementList`,
    method: "get",
    params: params
  });
}

/**
 * 获取指标与表关系列表
 * @param {*} data 
 * @returns 
 */
export function getIndicatorTableData(data) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/metadata/queryIndicatorTableData`,
    method: "post",
    data
  });
}
/**
 * 获取指标与表关系 - id
 * @param {*} params 
 * @returns 
 */
export function getIndicatorTableDataById(params) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/metadata/queryIndicatorTableDataById`,
    method: "get",
    params
  });
}
/**
 * 查询数据库类型
 * @param {*} params 
 * @returns 
 */
export function getDataBaseDataType(params) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/metadata/queryDataBaseDataType`,
    method: "get",
    params
  });
}

/**
 * 模型维护 - 保存
 * @param {*} data 
 * @returns 
 */
export function saveSelectedMetadata(data) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/metadata/saveSelectedMetadata`,
    method: "post",
    data
  });
}

/**
 * @Author: Jason Liu
 * @description: 安全数据保存
 */
export function secureSave(
  params = {
    id: undefined,
    datasourceId: undefined, // 目标数据源id
    schemaName: undefined, // 目标数据源schema
    tableId: undefined, // 表id
    tableCode: undefined, // 表code
    tableName: undefined, // 表name
    tableSno: undefined, // 表编码
    sourceDatasourceId: undefined, // 来源数据源id
    sourceSchemaName: undefined, // 来源数据源schema
    isManual: "1",
    columnList: [],
    indicatorList: []
  }
) {
  return new Promise((resolve, reject) => {
    const { indicatorList, columnList, tableCode, tableName, schemaName } = params;
    if (!tableCode) {
      reject("请输入模型名");
    } else if (!tableName) {
      reject("请输入模型中文名");
    } else if (columnList.length == 0) {
      reject("请设置模型字段信息");
    } else if (indicatorList.length == 0) {
      reject("请至少设置一条关联指标信息");
    } else if (!schemaName) {
      reject("请选择目标库");
    } else {
      return saveSelectedMetadata(params).then(resolve);
    }
  });
}

/**
 * 模型列表 - 删除
 * @param {*} data 
 * @returns 
 */
export function deleteIndicatorTableData(params) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/metadata/deleteIndicatorTableData`,
    method: "get",
    params
  });
}

/**
 * 校验是否为hdw公版表
 * @param {} params 
 * @returns 
 */
export function checkHdwCommonTable(params) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/metadata/isHdwCommonTable`,
    method: "get",
    params
  });
}

/**
 * 变更脚本
 * @param {*} params 
 * @returns 
 */
export function getHdwSql(params) {
  return $axios({
    url: `${$service.dataModelService}/api/v2/metadata/queryIndicatorTableUpdateSql`,
    method: "get",
    params
  });
}